package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataActionRotate extends DataAction implements ITag{
	public static String DEFAULT_ID="_ActionRotate";
		DataSize startAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,0);
		DataSize angle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,0);
		DataSize endAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,360);
public DataActionRotate(DataNode parent) {
	super(parent);

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_ANGLE))
		{
			startAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,0);
	  		startAngle.buildFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(eleName.equals(Token.ANGLE))
		{
			angle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,0);
			angle.buildFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(eleName.equals(Token.END_ANGLE))
		{
			endAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,360);
			endAngle.buildFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_ROTATE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_ANGLE))
		{
			startAngle.copyFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(eleName.equals(Token.ANGLE))
		{
			angle.copyFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(eleName.equals(Token.END_ANGLE))
		{
			endAngle.copyFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_ROTATE,""+element.getStartLineNo()} );
		}
	}
}
public DataSize getAngle() {
	return angle;
}
public DataAction getClone() {


	DataActionRotate newActionRotate = new DataActionRotate(parent);

	super.getClonedElements(newActionRotate);

	newActionRotate.startAngle = startAngle.getClone();
	newActionRotate.angle = angle.getClone();
	newActionRotate.endAngle = endAngle.getClone();

	return newActionRotate;
}
public DataAction getClone(String path) {


	DataActionRotate newActionRotate = new DataActionRotate(parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newActionRotate );

	super.getClonedElements(path,newActionRotate);

	newActionRotate.startAngle = startAngle.getClone(path);
	newActionRotate.angle = angle.getClone(path);
	newActionRotate.endAngle = endAngle.getClone(path);

	return newActionRotate;
}
public DataSize getEndAngle() {
	return endAngle;
}
public DataSize getStartAngle() {
	return startAngle;
}
protected void process()
{
 try{
	int newAngle = startAngle.getValue();
	int diffAngle = endAngle.getValue() - newAngle;
	newAngle += currentStep.getValue() * diffAngle / steps.getValue();
	angle.setValue(newAngle, this);
 }catch(LockException le){}
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(startAngle,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(angle,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(endAngle,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj,newObj,false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	super.replace(oldObj,newObj);

	if(startAngle==oldObj)
		startAngle=(DataSize)newObj;

	if(angle==oldObj)
		angle=(DataSize)newObj;

	if(endAngle==oldObj)
		endAngle=(DataSize)newObj;
}
public void resetChanged() {
	super.resetChanged();
	startAngle.resetChanged();
	angle.resetChanged();
	endAngle.resetChanged();
}
protected void setAngle(DataSize newAngle) {
	angle = newAngle;
}
protected void setEndAngle(DataSize newEndAngle) {
	endAngle = newEndAngle;
}
protected void setStartAngle(DataSize newStartAngle) {
	startAngle = newStartAngle;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += startAngle.togrfxML(Token.START_ANGLE);
	xml += angle.togrfxML(Token.ANGLE);
	xml += endAngle.togrfxML(new DataSize(360),Token.END_ANGLE);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
